# -*- coding: utf-8 -*-

import uuid
import time
import json

from django.http import HttpResponse
from django.template import loader
from cent import generate_token

import logging
logger = logging.getLogger('bt_server')

from base.settings import WEBSOCKET_CLIENT_URL, WEBSOCKET_SECRET, WEBSOCKET_CHANNLE


def index_view(requset):
    user = 'browser-{}'.format(uuid.uuid4().hex)
    timestamp = str(int(time.time()))
    info = json.dumps({'client': 'browser', 'name': 'browser-clent'})
    token = generate_token(WEBSOCKET_SECRET, user, timestamp, info=info)

    template = loader.get_template('index.html')
    return HttpResponse(
        template.render({
            'user': user,
            'url': WEBSOCKET_CLIENT_URL,
            'timestamp': timestamp,
            'token': token,
            'info': info,
            'channel': '{}:broadcast'.format(WEBSOCKET_CHANNLE)
        }))
